<template>
  <div class="shopping-page">
    <div class="cart" />
    <div class="products">
      <div v-for="product in list" :key="product.id" class="product">
        <img :src="product.image" alt="商品图片" class="imgSet">
        <h5>商品名：{{ product.item_name }}</h5>
        <p style="font-size:18px; color:red;">价格￥：{{ product.price }}</p>
        <p style="font-size:12px;">库存量：{{ product.quantity }}</p>
        <p style="font-size:12px;">商品描述：{{ product.description }}</p>
        <el-button class="deleteMyItem" round type="danger" icon="el-icon-shopping-cart-2" @click="deleteMyItem(product)">删除</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { deleteItemAPI, getMyItemAPI } from '@/api/item'
import router from '@/router'
import { Message } from 'element-ui'
export default {
  data() {
    return {
      list: [],
      cart: [],
      params: {
        item_id: ''
      },
      item: {
        item_id: '',
        item_name: '',
        description: '', // 描述
        price: '',
        quantity: '', // 数量
        image: '',
        seller_id: ''
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async deleteMyItem(product) {
      try {
        this.params.item_id = product.item_id
        await deleteItemAPI(this.params)
        await this.getList()
        console.log(product.item_id)
        this.$message.success('删除成功')
      } catch (error) {
        this.$message.error(error.response.data.msg)
      }
    },
    getList() {
      getMyItemAPI().then((res) => {
        this.list = res
        console.log(res)
      }).catch((error) => {
        if (error.response.status === 400) {
          router.push('/login')
          Message.error('登录过期')
        }
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.shopping-page {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.index-title {
  width: 100%;
  color: #ff4d4d;
  font-size: 36px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 20px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ededed;
  padding-bottom: 20px;

  .search-main {
    width: 320px;
    margin-right: 10px;
  }
}

.products {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.product {
  flex: 0 1 300px;
  margin: 5px;
  width: 300px;
  background-color: #f8f8f8;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;

  &:hover {
    transform: scale(1.05);
  }

  img {
    width: 100%;
    height: 150px;
    object-fit: cover;
    border-radius: 8px;
  }

  h3 {
    font-size: 18px;
    margin: 10px 0;
  }

  .price {
    font-size: 16px;
    color: #e44d26;
  }

  .quantity, .description {
    font-size: 14px;
    margin: 5px 0;
  }

  .deleteMyItem {
    margin-top: 10px;
  }
}

.cart {
  margin-top: 20px;
}
</style>
